package com.jk.lzy.service.Impl;

import com.jk.*;
import com.jk.lzy.Utils.AddressUtils;
import com.jk.lzy.dao.MovieInfoDao;
import com.jk.lzy.service.MovieInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 梁展艺
 * @Description: 选座购票 业务层
 * @Date: 2021/5/21 10:25
 * @param null
 * @Return:
 **/
@Service
public class MovieInfoServiceImpl implements MovieInfoService {
    @Autowired
    private MovieInfoDao lzyTicketDao;
    // 注入 Mongodb
    @Autowired
    private MongoTemplate mongoTemplate;
    // 注入 redis
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @Author: 梁展艺
     * @Description: 购票 影院信息回显
     * @Date: 2021/5/21 16:24
     * @param cinemaId
     * @Return: com.jk.CinemaBean
     **/
    @Override
    public CinemaBean chooseByCinemaId(Integer cinemaId) {
        CinemaBean cinemaBean = lzyTicketDao.chooseByCinemaId(cinemaId);
        return cinemaBean;
    }
    /**
     * @Author: 梁展艺
     * @Description: 首页点击购票  展示影院详情
     * @Date: 2021/5/21 16:24
     * @param movieId
     * @Return: com.jk.MovieBean
     **/
    @Override
    public MovieBean movieDetailsBy(Integer movieId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("movieId").is(movieId));
        Integer count = Math.toIntExact(mongoTemplate.count(query, DiscussBean.class));
        MovieBean movie =  lzyTicketDao.movieDetailsBy(movieId);
        movie.setMovidCountLun(count); // 加了 虚拟 数据
        return movie;
    }
    /**
     * @Author: 梁展艺
     * @Description: 购票 根据 市 查询 影院
     * @Date: 2021/5/21 20:34
     * @param cinema
     * @Return: java.util.List<com.jk.CinemaBean>
     **/
    @Override
    public List<CinemaBean> cinemaByCity(CinemaBean cinema) {
        // 调接口 获取当前的 地区
        String city = AddressUtils.addressByIpCity();
        if(city != null && city != ""){
            cinema.setCity(city);
        }else{
            cinema.setCity("西安");
        }
        List<CinemaBean> list =  lzyTicketDao.cinemaByCity(cinema);
        return list;
    }
    /**
     * @Author: 梁展艺
     * @Description:  根据影院查看电影
     * @Date: 2021/5/24 19:39
     * @param cinemaId
     * @Return: java.util.List<com.jk.MovieBean>
     **/
    @Override
    public MovieBean movieByCinema(Integer movieId) {
        // 电影 id 查询 电影
        MovieBean movie1 = lzyTicketDao.movieByMovieId(movieId);
        return movie1;
    }
    /**
     * @Author: 梁展艺
     * @Description: 影院电影放映 安排
     * @Date: 2021/5/25 20:09
     * @param program
     * @Return: java.util.List<com.jk.ProgramBean>
     **/
    @Override
    public List<ProgramBean> moviePlan(ProgramBean program) {
        List<ProgramBean> list = lzyTicketDao.moviePlan(program);
        for (ProgramBean programBean:list) {
            programBean.setPlayStartTime(programBean.getPlayStartTime().substring(0,5));
            programBean.setPlayEndTime(programBean.getPlayEndTime().substring(0,5));
        }
        return list;
    }
    /**
     * @Author: 梁展艺
     * @Description: 选座 回显放映计划
     * @Date: 2021/5/26 11:29
     * @param programId
     * @Return: com.jk.ProgramBean
     **/
    @Override
    public ProgramBean programByprogramId(Integer programId) {
        ProgramBean program =  lzyTicketDao.programByprogramId(programId);
        program.setPlayStartTime(program.getPlayStartTime().substring(0,5));
        return program;
    }
    /**
     * @Author: 梁展艺
     * @Description: 座位描述
     * @Date: 2021/5/27 10:30
     * @param programId
     * @Return: com.jk.MovieHallBean
     **/
    @Override
    public HashMap seatInfo(Integer programId) {
        MovieHallBean movieHall = lzyTicketDao.programByprogram(programId); // 获取 影院id和影厅id
        // 定义 公共的list
        List<SeatBean> listSeat = new ArrayList<>();
        //数据存 redis
        String key = "seatInfo"; // 顶级的key
        String keyHash = movieHall.getCinemaId()+""+movieHall.getHallId();
        if (redisTemplate.opsForHash().hasKey(key,keyHash)){
            listSeat = (List<SeatBean>) redisTemplate.opsForHash().get(key, keyHash);
        }else{
            // 根据 影院id 和 影厅id 查询出座位的详情
            listSeat = lzyTicketDao.findSeatByCinemaId(movieHall.getHallId(),movieHall.getCinemaId());
            redisTemplate.opsForHash().put(key,keyHash,listSeat);
            redisTemplate.expire(key,5, TimeUnit.MINUTES);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("movieHall",movieHall);
        map.put("listSeat",listSeat);
        return map;
    }
    /**
     * @Author: 梁展艺
     * @Description: 生成订单
     * @Date: 2021/5/28 21:33
     * @param programId
     * @Return: java.lang.String
     **/
    @Override
    public OrderBean createOrder(Integer programId,String state,Double orderPrice,UserBean user) {
        OrderBean order = lzyTicketDao.findUserBuyOrder(programId);
        order.setPhone(user.getPhone()); // 用户昵称
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        order.setDate(simpleDateFormat.format(new Date())); // 订单 时间
        order.setOrderPrice(orderPrice); //订单金额
        // 生成订单编号
        long time = new Date().getTime();
        order.setOrderId(time+"");
        order.setSeats(state); // 订单 座位
        // 修改座位 状态
        String[] split = state.split(",");
        // Redis key hashkey
        String key = "seatInfo"; // 顶级的key
        String keyHash = order.getCinemaId()+""+order.getHallId();
        List<SeatBean> listSeat = (List<SeatBean>) redisTemplate.opsForHash().get(key, keyHash);
        for (int i = 0; i < split.length; i++) {
            String seateInfo =  split[i].substring(0,1)+""+split[i].substring(3,4);
            for (SeatBean seatBean:listSeat) {
                String cc = seatBean.getXy() + "";
                if (seateInfo.equals(cc)){
                    seatBean.setStatus(1);
                }
            }
        }
        // 继续 存入redis
        redisTemplate.opsForHash().put(key,keyHash,listSeat);
        redisTemplate.expire(key,5, TimeUnit.MINUTES); // 设置过期时间 5分钟
        // 存 订单
        String orderKey = "orderList";
        String orderHashKey = "userphone_"+order.getPhone();
        if (redisTemplate.opsForHash().hasKey(orderKey,orderHashKey)){
            List<OrderBean> listOrder = (List<OrderBean>) redisTemplate.opsForHash().get(orderKey, orderHashKey);
            listOrder.add(order);
        }else{
            List<OrderBean> orderList = new ArrayList<>();
            orderList.add(order); // 将未支付的订单 存入redis
            redisTemplate.opsForHash().put(orderKey,orderHashKey,orderList);
        }
        // 定时任务
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                redisTemplate.opsForHash().delete(orderKey,orderHashKey);
            }
        },300000);
        return order;
    }
    /**
     * @Author: 梁展艺
     * @Description: 限时支付 页面 订单描述
     * @Date: 2021/5/31 16:13
     * @param orderBean
     * @Return: com.jk.OrderBean
     **/
    @Override
    public OrderBean orderInfoByOrder(OrderBean orderBean) {
        // 存 订单
        String orderKey = "orderList";
        String orderHashKey = "userphone_"+orderBean.getPhone();
        OrderBean orderBean1 = new OrderBean();
        List<OrderBean> orderList  = (List<OrderBean>) redisTemplate.opsForHash().get(orderKey, orderHashKey);
        for (OrderBean orderBean2:orderList) {
            if (orderBean.getOrderId().equals(orderBean2.getOrderId())){
                orderBean1 = orderBean2;
            }
        }
        return orderBean1;
    }
}
